<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="材料描述" prop="describes">
        <el-input
          v-model="queryParams.describes"
          placeholder="请输入材料描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传人" prop="uploadBy">
        <el-input
          v-model="queryParams.uploadBy"
          placeholder="请输入上传人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传时间" prop="uploadTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.uploadTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择上传时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-switch
          v-model="queryParams.showResult"
          active-text="发布评分结果"
          v-hasPermi="['system/user/index']"
          @change = "handleShowResult"
        >
        </el-switch>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['material:upload:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['material:upload:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['material:upload:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['material:upload:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="uploadList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="材料编号" align="center" prop="materialId" width="100" sortable/>
      <el-table-column label="材料描述" align="center" prop="describes"/>
      <el-table-column label="党支部" align="center" prop="deptName"/>
      <el-table-column label="上传人" align="center" prop="uploadBy" width="130" sortable/>
      <el-table-column label="上传时间" align="center" prop="uploadTime" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.uploadTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总得分" align="center" prop="score" width="100" sortable/>
      <el-table-column label="打分人数" align="center" prop="number" width="100" sortable/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-view"
            @click="handlePreview(scope.row)"
          >预览
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDownload(scope.row)"
          >下载
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['material:upload:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['material:upload:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改上传材料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="材料描述" prop="describes">
          <el-input v-model="form.describes" placeholder="请输入材料描述"/>
        </el-form-item>
        <el-form-item label="材料路径">
          <fileUpload v-model="form.fileName"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUpload, getUpload, delUpload, addUpload, updateUpload,getShowResult,updateShowResult } from '@/api/material/upload'

export default {
  name: 'Upload',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 上传材料表格数据
      uploadList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        describes: null,
        uploadBy: null,
        uploadTime: null,
        showResult: false
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    }
  },
  created() {
    this.getSwitch()
    this.getList()
  },
  methods: {
    /** 查询上传材料列表 */
    getList() {
      this.loading = true
      listUpload(this.queryParams).then(response => {
        this.uploadList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        materialId: null,
        deptId: null,
        describes: null,
        fileName: null,
        filePath: null,
        uploadBy: null,
        uploadTime: null,
        score: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.materialId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加上传材料'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const materialId = row.materialId || this.ids
      getUpload(materialId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改上传材料'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        console.log('form')
        console.log(this.form)
        if (valid) {
          if (this.form.materialId != null) {
            updateUpload(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addUpload(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialIds = row.materialId || this.ids
      this.$modal.confirm('是否确认删除上传材料编号为"' + materialIds + '"的数据项？').then(function() {
        return delUpload(materialIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('material/upload/export', {
        ...this.queryParams
      }, `upload_${new Date().getTime()}.xlsx`)
    },
    // 文件下载处理
    handleDownload(row) {
      let name = row.fileName
      let nameList = name.split(',')
      // 循环下载多个文件
      for (let i = 0; i < nameList.length; i++) {
        console.log(nameList[i])
        let url = process.env.VUE_APP_BASE_API + nameList[i]
        console.log(url)
        let suffix = url.substring(url.lastIndexOf('.'), url.length)
        console.log(suffix)
        const a = document.createElement('a')
        a.setAttribute('download', row.uploadTime + '_' + row.uploadBy + suffix)
        // a.setAttribute('target', '_blank')  打开新页面下载
        a.setAttribute('href', url)
        a.click()
      }
      this.$modal.msgSuccess('开始下载')
    },
    /** 预览按钮 */
    handlePreview(row) {
      let name = row.fileName
      let url = process.env.VUE_APP_BASE_API + row.fileName
      let nameList = name.split(',')
      if (nameList.length > 1) {
        this.$modal.msgError('该文件不支持预览')
      } else {
        let suffix = url.substring(url.lastIndexOf('.'), url.length)
        if (suffix === '.png' || suffix === '.jpg' || suffix === '.jpeg'
          || suffix === '.gif' || suffix === '.pdf') {
          window.open(url)
        } else {
          this.$modal.msgError('该文件不支持预览')
        }

      }
      //window.open("https://view.officeapps.live.com/op/view.aspx?src="+url)
    },
    /** 查询结果开关状态 */
    getSwitch(){
      getShowResult().then(response => {
        this.queryParams.showResult = (response.data === 1)
        console.log("getShowResult")
        console.log(this.queryParams.showResult)
      })
    },
    /** 改变开关 */
    handleShowResult(){
      console.log(this.queryParams.showResult)
      updateShowResult(this.queryParams.showResult).then(response => {
      })
      this.getList()
    }
  }
}
</script>
